package classes;

import interfaces.IAfficheur;
import interfaces.ICanal;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Point;
import java.util.concurrent.ScheduledFuture;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class AfficheurImpl extends JFrame implements IAfficheur{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JLabel lbl_version;
	private JLabel lbl_Val;
	private ScheduledFuture<?> future=null;

	public void buildUI(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 328, 218);
		contentPane = new JPanel();
		contentPane.setBackground(Color.LIGHT_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblCapteur = new JLabel("Afficheur");
		lblCapteur.setBounds(129, 11, 97, 20);
		lblCapteur.setFont(new Font("Tahoma", Font.BOLD, 16));
		contentPane.add(lblCapteur);
		
		JPanel panel = new JPanel();
		panel.setForeground(Color.WHITE);
		panel.setBackground(Color.WHITE);
		panel.setBounds(129, 53, 85, 51);
		contentPane.add(panel);
		
		lbl_Val = new JLabel("/");
		panel.add(lbl_Val);
		lbl_Val.setBackground(Color.GREEN);
		lbl_Val.setFont(new Font("Tahoma", Font.BOLD, 37));
		
		JLabel lblVersionValeur = new JLabel("Version Valeur:");
		lblVersionValeur.setBounds(10, 138, 91, 14);
		contentPane.add(lblVersionValeur);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(111, 121, 115, 31);
		contentPane.add(panel_1);
		
		lbl_version = new JLabel("Pas de version");
		lbl_version.setSize(200, 50);
		panel_1.add(lbl_version);
	}

	public AfficheurImpl() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					buildUI();
					setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

	@Override
	public void update(ICanal c1) {
		if(future==null){
			future=c1.getValue();
		}
		else{
			future=c1.getValue();
		}
		
		try {
			Point value=(Point) future.get();
			afficherValue(value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ScheduledFuture<?> getFuture() {
		return future;
	}

	private void afficherValue(Point value) {
		lbl_Val.setText(value.x+"");	
		//si le num de version est diff. de 0 alors il s'agit de la strategie par epoque
		if(value.y!=0){
			lbl_version.setText(value.y+"");
		}
	}
}
